package com.mine.common.core.listener;

import com.mine.common.core.cache.DefaultMemoryCacheOperator;
import com.mine.common.core.constants.Constants;
import com.mine.common.core.request.dto.UserVO;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * 个人信息鼠标点击监听
 *
 * @author WangYX
 * @version 1.0.0
 * @date 2023/11/29 16:42
 */
public class PersonInfoMouserListener implements MouseListener {

    private final JLayeredPane contentArea;

    private final JPanel panel;

    public PersonInfoMouserListener(JLayeredPane contentArea, JPanel panel) {
        this.contentArea = contentArea;
        this.panel = panel;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        remove();
        repaint();
        data();
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        Color color = new Color(236, 239, 255);
        panel.setBackground(color);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        Color color = new Color(251, 248, 255);
        panel.setBackground(color);
    }

    /**
     * 移除所有组件
     */
    public void remove() {
        contentArea.removeAll();
    }

    /**
     * 重新绘制
     */
    private void repaint() {
        contentArea.repaint();
    }

    /**
     * 添加数据
     */
    public void data() {
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(5, 5, 300, 500);
        Color color = new Color(255, 255, 255);
        panel.setBackground(color);

        UserVO user = (UserVO) DefaultMemoryCacheOperator.get(Constants.USER_KEY);

        addLabel(panel, "姓名", user.getName(), 10);
        addLabel(panel, "昵称", user.getNickname(), 50);
        addLabel(panel, "年龄", user.getAge().toString(), 90);
        addLabel(panel, "性别", user.getSex(), 130);
        addLabel(panel, "地区", user.getAreaId() == null ? "" : user.getAreaId().toString(), 170);
        addLabel(panel, "详细地址", user.getAddress(), 210);
        addLabel(panel, "教派", user.getReligiousSectId() == null ? "" : user.getReligiousSectId().toString(), 250);
        addLabel(panel, "天谴", user.getWrathOfHeavenId() == null ? "" : user.getWrathOfHeavenId().toString(), 290);
        addLabel(panel, "种族", user.getRaceId() == null ? "" : user.getRaceId().toString(), 330);
        addLabel(panel, "境界", user.getRealmId() == null ? "" : user.getRealmId().toString(), 370);
        addLabel(panel, "体质", user.getConstitutionId() == null ? "" : user.getConstitutionId().toString(), 410);
        addLabel(panel, "生辰", user.getBirthBaZi(), 450);

        contentArea.add(panel, JLayeredPane.MODAL_LAYER);

    }

    private void addLabel(JPanel panel, String name, String value, int y) {
        Font font = new Font("微软雅黑", Font.PLAIN, 16);

        JLabel label = new JLabel(name + "：");
        label.setBounds(20, y, 100, 30);
        label.setFont(font);
        panel.add(label);

        JLabel labelValue = new JLabel(value);
        labelValue.setBounds(110, y, 100, 30);
        labelValue.setFont(font);
        panel.add(labelValue);
    }
}
